package me.raih.controller.sales;

import me.raih.pojo.po.Bill;
import me.raih.pojo.po.User;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.Manager;
import me.raih.service.BillService;
import me.raih.service.UserService;
import me.raih.service.WaresService;
import me.raih.util.CaseBeanUtil;
import me.raih.util.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Raih
 * @Time 2020/12/29 14:20
 *
 * 关于售货员 页面 跳转
 */
@Controller
@RequestMapping(value = "/sales")
public class SalesPageController {

    @Autowired
    BillService billService;

    @Autowired
    WaresService waresService;

    @Autowired
    UserService userService;

    @Autowired
    Manager manager;

    @RequestMapping(value = "/shop/sale")
    public String sale(Model model){

        Map<String, Double> forSale = manager.getForSale();
        String next = forSale.keySet().iterator().next();
        if("YES".equals(next)){
            model.addAttribute("FORSALE", MathUtil.twoDoubleNumber(forSale.get(next)));
        }

        return "sales/sale";
    }

    @RequestMapping(value = "/shop/bill")
    public String bill(){

        return "sales/bill";
    }


    @RequestMapping(value = "/shop/leave")
    public String leave(){

        return "sales/leave";
    }

    @RequestMapping(value = "/shop/salary")
    public String salary(){

        return "sales/salary";
    }

    @RequestMapping(value = "/shop/download")
    public String download(){

        return "sales/download";
    }


    @RequestMapping(value = "/bill/showWaresMessage")
    public String toWaresMessage (HttpServletRequest request,
                                  Model model,
                                  @RequestParam("billNo") String billNo){

        Object bill_wares_message = request.getSession().getAttribute("BILL_WARES_MESSAGE");
        List<Bill> billList = CaseBeanUtil.castBean(bill_wares_message, Bill.class);
        //存放所有的商品信息
        List<Wares> waresList = new ArrayList<>();
        for (Bill bill : billList) {
            //如果两个订单号相同，就是我们要找的
            if(bill.getBillNo() .equals(billNo)){
                //通过该订单获取该订单下的商品信息
                Map<String, Integer> map = bill.getSellWaresMap();
                //遍历所有的 key 和 value
                for (String key :map.keySet()) {
                    //通过商品号查询商品信息
                    Wares wares = waresService.findWaresByNo(key);
                    //商品的数量信息不是库存数量，而是客户购买的数量，也就是map对应的value
                    int number = map.get(key);
                    wares.setNumber(number);
                    //设置商品总价，把进价改成总价
                    wares.setTradePrice(number*wares.getRetailPrice());
                    //积分改成总积分
                    wares.setPoints(wares.getPoints()*number);
                    //添加到集合中
                    waresList.add(wares);
                }

                //传到前台
                request.setAttribute("BILL_WARES_MESSAGE",waresList);
            }
        }

        return "sales/waresMessage";
    }


    @RequestMapping(value = "/bill/showUserMessage")
    public String toUserMessage(HttpServletRequest request,
                                @RequestParam("username")String username){


        //通过用户名查找用户
        User user = userService.findUserByUsername(username);

        request.setAttribute("BILL_USER_MESSAGE",user);

        return "sales/userMessage";
    }

}
